package com.atguigu.algorithm.lru;

import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;

/**
 * LRU 算法
 * @author xr-8
 * @create 2021-01-28 21:50
 */
public class LRUCacheDemo {

    //map负责查找，构造一个虚拟的双向链表，它里面安装的就是一个个node节点，作为数据载体

    //1,构造Node节点
    class Node<K,V>{
        K key;
        V value;
        Node<K,V> prev;
        Node<K,V> next;

        public Node(){
            this.prev = this.next = null;
        }

        public Node(K key, V value) {
            this.key = key;
            this.value = value;
            this.prev = this.next = null;
        }
    }

    //2，构造一个虚拟的双向链表，安放的是Node节点
    class DoubleLinkedList<K,V>{
        Node<K,V> head;
        Node<K,V> tail;

        //2.1构造函数
        public DoubleLinkedList(){
            this.head = new Node<>();
            this.tail = new Node<>();
            this.head.next = this.tail;
            this.tail.prev = this.head;
        }

        //2.2添加到头
        public void addHead(Node<K,V> node){
            node.next = this.head.next;
            node.prev = this.head;
            this.head.next.prev = node;
            this.head.next = node;
        }

        //2.3删除节点
        public void removeNode(Node<K,V> node){
           node.next.prev = node.prev;
           node.prev.next = node.next;
           node.prev = null;
           node.next = null;
        }

        //2.4获得最后一个节点
        public Node getLast(){
            return this.tail.prev;
        }
    }

    private int cacheSize;
    Map<Integer, Node<Integer, Integer>> map;
    DoubleLinkedList<Integer, Integer> doubleLinkedList;

    public LRUCacheDemo(int cacheSize){
        this.cacheSize = cacheSize;//坑位
        this.map = new HashMap<>();//查找
        this.doubleLinkedList = new DoubleLinkedList<>();
    }

    public int get(int key){
        if(!map.containsKey(key)){
            return -1;
        }

        Node<Integer, Integer> node = map.get(key);
        this.doubleLinkedList.removeNode(node);
        this.doubleLinkedList.addHead(node);

        return node.value;
    }

    public void put(int key, int value){
        if(map.containsKey(key)){//update
            Node<Integer, Integer> node = map.get(key);
            node.value = value;
            map.put(key, node);

            this.doubleLinkedList.removeNode(node);
            this.doubleLinkedList.addHead(node);
        }else{
            if(map.size() == cacheSize){
                Node<Integer, Integer> lastNode = this.doubleLinkedList.getLast();
                map.remove(lastNode.key);
                this.doubleLinkedList.removeNode(lastNode);
            }
            //add
            Node<Integer, Integer> newNode = new Node<>(key, value);
            map.put(key, newNode);
            this.doubleLinkedList.addHead(newNode);
        }
    }


    public static void main(String[] args) {
        LRUCacheDemo lRUCacheDemo = new LRUCacheDemo(3);
        lRUCacheDemo.put(1,10);
        lRUCacheDemo.put(2,20);
        lRUCacheDemo.put(3,30);
        System.out.println(lRUCacheDemo.map.keySet());
        lRUCacheDemo.put(4,10);
        System.out.println(lRUCacheDemo.map.keySet());
        lRUCacheDemo.put(3,10);
        System.out.println(lRUCacheDemo.map.keySet());
        lRUCacheDemo.put(3,10);
        System.out.println(lRUCacheDemo.map.keySet());
        lRUCacheDemo.put(3,10);
        System.out.println(lRUCacheDemo.map.keySet());
        lRUCacheDemo.put(5,10);
        System.out.println(lRUCacheDemo.map.keySet());
        lRUCacheDemo.put(6,10);
        System.out.println(lRUCacheDemo.map.keySet());
    }
}
